import http from "../api/http.js";
import { adornUrl, adornParams } from "../tools/index.js";

// 生成路由权限
function CreatePermission() {
  // console.log(permission);
  const a = [
    {
      path: "/",
      name: "home",
      component: () =>
        import(/* webpackChunkName: "Hello" */ "../views/Home.vue"),
      children: [
        {
          path: "/xs",
          name: "xs",
          component: () =>
            import(/* webpackChunkName: "xsgl" */ "../views/xsgl.vue")
        },
        {
          path: "/cw",
          name: "xs",
          component: () =>
            import(/* webpackChunkName: "cwgl" */ "../views/cwgl.vue")
        },
        {
          path: "/xz",
          name: "xz",
          meta: { token: "admin" },
          component: () =>
            import(/* webpackChunkName: "xzgl" */ "../views/xzgl.vue")
        }
        // {
        //   path: "/test",
        //   name: "test",
        //   meta: { token: "admin" },
        //   component: () =>
        //     import(/* webpackChunkName: "test" */ "../views/test.vue")
        // }
      ]
    }
  ];
  return a;
}

export default {
  state: {
    permission: [],
    userInf: {} //用户信息
  },
  mutations: {
    setPermission(state, data) {
      state.permission = data;
    },
    setUserInf(state, data) {
      // console.log("setUserInf");
      // console.log(data);

      state.userInf = data;
    }
  },
  actions: {
    // 获取权限列表
    getPermission({ commit }) {
      http({
        // url: adornUrl("/sys/childmenu/nav"),//原来
        url: adornUrl("/sys/AdvancedInfo/infoAndNav"),
        method: "get",
        params: adornParams({
          name: "智慧长兴农业综合管理门户"
        })
      })
        .then(({ data }) => {
          const code = data.code;
          const inf = data.information;

          // console.log(inf);

          if (inf && code === 0) {
            commit("setPermission", inf.permission); //设置权限
            commit("setUserInf", {
              department: inf.department,
              duty: inf.duty,
              head: inf.head,
              name: inf.name,
              num: inf.num
            });
          } else {
            console.log("获取失败");
          }
        })
        .catch(e => {
          console.log(
            `%c${e} 请求菜单列表和权限失败，跳转至登录页！！`,
            "color:blue"
          );
        });
    },
    getPermissionTest({ commit }, data) {
      commit("setPermission", data);
    },
    // 获取用户信息
    getUserInf({ commit }, data) {
      commit("setUserInf", data);
    }
  },
  getters: {
    getPermission2({ permission }) {
      return CreatePermission(permission); // 生成路由权限
    },
    getPermission({ permission }) {
      let menuTree = [];

      permission.forEach((item, index) => {
        menuTree.push({
          img: `/portal/images/sm_icon0${index + 1}.png`,
          index: item.path,
          title: item.name
        });
      });
      menuTree.push({
        img: "/portal/images/sm_icon06.png",
        index: "55",
        title: "个人设置",
        subs: [
          {
            index: "/config",
            title: "个人配置"
          },
          {
            index: "551",
            title: "IP锁定"
          },
          {
            index: "5523",
            title: "登录记录"
          },
          {
            index: "5525",
            title: "背景设置"
          }
        ]
      });

      return menuTree;
    }
  }
};
